﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System.Collections;
using System.Collections.Generic;
using ZO.SmartCore.Collections.Generic;

#endregion

namespace ZO.SmartCore.Configuration
{
    /// <summary>
    /// Represents a group of section within a configuration file.
    /// </summary>
    public sealed class SectionCollection : IEnumerable<Section>
    {

        #region Constructors

        #endregion

        #region Destructor

        #endregion

        #region Fields
        private OrderedDictionary<string, Section> sections = new OrderedDictionary<string, Section>();

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties
        /// <summary>Gets the <see cref="System.Collections.Generic.IEqualityComparer{T}"></see> that is used to determine equality of sections for the dictionarytable. </summary>
        /// <returns>The <see cref="System.Collections.Generic.IEqualityComparer{T}"></see> generic interface implementation that is used to determine equality of sections for the current <see cref="T:System.Collections.Generic.Dictionary`2"></see> and to provide hash values for the sections.</returns>
        public IEqualityComparer<string> Comparer
        {
            get
            {
                return this.sections.Comparer;
            }
        }

        /// <summary>
        /// Gets the count.
        /// </summary>
        /// <value>The count.</value>
        public int Count
        {
            get
            {
                return this.sections.Count;
            } // get
        }

        /// <summary>
        /// Gets an collection containing the values of the <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"></see>.
        /// </summary>
        /// <value></value>
        /// <returns>An collection containing the values of the object that implements <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"></see>.</returns>
        public ICollection<string> Keys
        {
            get
            {
                return sections.Keys;
            }
        }

        /// <summary>
        /// Gets an collection containing the values in the <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"></see>.
        /// </summary>
        /// <value></value>
        /// <returns>An collection containing the values in the object that implements <see cref="System.Collections.Generic.IDictionary{TKey, TValue}"></see>.</returns>
        public ICollection<Section> Values
        {
            get
            {
                return sections.Values;
            } // get
        }

        /// <summary>
        /// Gets or sets the value with the specified key.
        /// </summary>
        /// <value></value>
        public Section this[string key]
        {
            get
            {
                return this.sections[key];
            }
            set
            {
                this.sections[key] = value;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Adds the key with specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        public void Add(string name)
        {
            sections.Add(name, new Section(name));
        } // Add

        /// <summary>
        /// Adds the key with specified name.
        /// </summary>
        /// <param name="newSection">The new section.</param>
        public void Add(Section newSection)
        {
            sections.Add(newSection.Name, newSection);
        } // Add

        /// <summary>
        /// Adds the key with specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="comment">The comment.</param>
        public void Add(string name, string comment)
        {
            sections.Add(name, new Section(name, comment));
        } // Add

        /// <summary>
        /// Removes the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        public void Remove(string name)
        {
            sections.Remove(name);
        } // Add

        /// <summary>
        /// Removes all sections and values from the Dictionary. 
        /// </summary>
        public void Clear()
        {
            sections.Clear();
        } // Clear

        /// <summary>
        /// Determines whether the dictionary contains specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>
        /// 	<c>true</c> if the specified key contains key; otherwise, <c>false</c>.
        /// </returns>
        public bool ContainsKey(string key)
        {
            return sections.ContainsKey(key);
        } // ContainsKey

        /// <summary>
        /// Determines whether the specified value contains value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>
        /// 	<c>true</c> if the specified value contains value; otherwise, <c>false</c>.
        /// </returns>
        public bool ContainsValue(Section value)
        {
            return this.sections.ContainsValue(value);
        } // ContainsValue

        /// <summary>
        /// Determines whether [contains] [the specified key].
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>
        /// 	<c>true</c> if [contains] [the specified key]; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(string key)
        {
            return sections.ContainsKey(key);
        } // ContainsKey

        /// <summary>
        /// Tries the get value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public bool TryGetValue(string key, out Section value)
        {
            return sections.TryGetValue(key, out value);
        } // TryGetValue
        #endregion

        #region IEnumerable<Section> Members

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<Section> GetEnumerator()
        {
            return this.sections.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.sections.GetEnumerator();
        }

        #endregion

    }
}
